package com.tal.pad.javabase.arithtest;

import java.util.Arrays;
import java.util.Collections;
import java.util.Random;

public class SortTest {

    public static Random rand = new Random();


    public static void main(String[] args) {
        int[] arr = genRandArr(100, 100);
        printArr(arr);
//        int[] newArr = genRandArr(100,100);

        int [] newArr = bubbleSore(arr);

        printArr(newArr);

        Arrays.sort(arr);

        printArr(arr);

        System.out.println("duiArr:" + duiArr(arr, newArr));
    }

    public static int[] bubbleSore(int [] arr){
        if(arr ==null || arr.length<1){
            return arr;
        }
        for(int i = arr.length-1; i >=0; i--){
            for(int j = 0; j<i; j++){
                if(arr[j]>arr[j+1]){
                    swipe(arr,j,j+1);
                }
            }
        }
        return arr;
    }

    public static void swipe(int[] arr, int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }







    public static boolean duiArr(int [] originalArr, int [] newArr){
        if(originalArr.length != newArr.length){
            return false;
        }
        for(int i =0; i<originalArr.length; i++){
            if(originalArr[i]!=newArr[i]){
                return false;
            }
        }
        return true;
    }

    public static int[] genRandArr(int len, int scope){
        int [] arr = new int[len];
        for(int i =0; i<len; i++){
            arr[i] = rand.nextInt(scope);
        }
        return arr;
    }



    public static void printArr(int [] arr){
        System.out.println("");
        for(int i =0; i<arr.length; i++){
            System.out.print(arr[i] +" ");
        }
        System.out.println();
    }

}
